package com.czkevin.cif;

import com.czkevin.cif.utils.ComponentUtils;
import de.tr7zw.nbtapi.*;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public abstract class BaseHeadItem implements CIFItem {

    @Override
    public String type() {
        return getClass().getSimpleName();
    }

    public abstract int[] getHeadOwner();
    public abstract String getHeadTexture();

    @Override
    public NBTContainer generateItemNBT(CIFItemInstance instance, boolean isBlock) {
        NBTContainer nbt = new NBTContainer();
        nbt.setString("id", Material.PLAYER_HEAD.getKey().toString());
        nbt.setByte("Count", (byte) 0x01);
        {
            NBTCompound tag = nbt.addCompound("tag");
            if(isUnbreakable()) tag.setInteger("Unbreakable", 1);
            tag.setByte("HideFlags", (byte) 0x63);

            {
                // Head
                NBTCompound skull = tag.addCompound("SkullOwner");
                skull.setIntArray("Id", getHeadOwner());
                NBTCompound props = skull.addCompound("Properties");
                NBTCompoundList textures = props.getCompoundList("textures");
                NBTListCompound tex = textures.addCompound();
                tex.setString("Value", getHeadTexture());
            }

            {
                NBTCompound display = tag.addCompound("display");
                String text = getDisplayName(instance != null ? instance.data : null);
                if(text == null) text = type();
                display.setString("Name", ComponentSerializer.toString(
                    ComponentUtils.cancelItalic(TextComponent.fromLegacyText(text))
                ));

                // set lore
                List<String> listLore = getLore(instance);
                if(!isBlock && listLore != null && listLore.size() > 0) {
                    NBTList<String> nbtList = display.getStringList("Lore");
                    listLore.forEach(s -> {
                        nbtList.add(ComponentSerializer.toString(TextComponent.fromLegacyText(s)));
                    });
                }
            }

            CustomItemFramework.putCIFData(tag.addCompound(CustomItemFramework.NBT_TAG_ENTRY), this, instance == null ? null : instance.data);
        }
        return nbt;
    }

    @Override
    public final boolean isSimilar(CIFItemInstance itemA, CIFItemInstance itemB) {
        if(itemA == null || itemB == null) return false;
        return itemA.type == itemB.type;
    }

    @Override
    public final ItemStack generateItemStack(CIFItemInstance instance, boolean isBlock) {
        NBTCompound nbt = generateItemNBT(instance, isBlock);
        return NBTItem.convertNBTtoItem(nbt);
    }

    @Override
    public final boolean isBlock() {
        return false;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName();
    }
}
